#!/usr/bin/python2.4
# Copyright 2009 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ========================================================================

Import('env')


local_env = env.Clone(
    COMPONENT_STATIC = False,
)

for omaha_version_info in local_env['omaha_versions_info']:
  prefix = omaha_version_info.filename_prefix

  for lang in omaha_version_info.GetSupportedLanguages():
    lang_env = local_env.Clone()
    lang_env.Append(
        RCFLAGS = [
            '/DVERSION_MAJOR=%d' % omaha_version_info.version_major,
            '/DVERSION_MINOR=%d' % omaha_version_info.version_minor,
            '/DVERSION_BUILD=%d' % omaha_version_info.version_build,
            '/DVERSION_PATCH=%d' % omaha_version_info.version_patch,
            '/DVERSION_NUMBER_STRING=\\"%s\\"' % (
                omaha_version_info.GetVersionString()),
            '/DLANGUAGE_STRING=\\"%s\\"' % lang
            ],
        LINKFLAGS = [
            '/ENTRY:DllEntry',
            '/MERGE:.rdata=.text',
            '/BASE:0x19000000',
            ],
        LIBS = [
          lang_env['crt_libs'][lang_env.Bit('debug')],
        ]
    )

    # Avoid conflicts in obj targets
    lang_env['OBJSUFFIX'] = '_' + lang + lang_env['OBJSUFFIX']
    lang_env['OBJPREFIX'] = '%s/%s' % (lang, prefix) + lang_env['OBJPREFIX']

    lang_base_name = 'generated_resources_' + lang
    lang_res = lang_env.RES(
        target='%s%s.res' % (prefix, lang_base_name),
        source='goopdateres/%s.rc' % (lang_base_name),
    )

    # Force a rebuild when the version changes.
    lang_env.Depends(lang_res, '$MAIN_DIR/VERSION')

    lang_inputs = [
        'resdll_main.cc',
        lang_res,
        # Needed to prevent rebuilding of the lib.
        'resource_only_dll.def'
        ]

    # Build the (unsigned) DLL
    unsigned_dll = lang_env.ComponentLibrary(
        lib_name='%s/%sgoopdateres_unsigned_%s' % (lang, prefix, lang),
        source=lang_inputs
    )

    signed_dll = lang_env.SignedBinary(
        target='%s/%sgoopdateres_%s.dll' % (lang, prefix, lang),
        source=unsigned_dll,
    )

    env.Replicate('$STAGING_DIR', signed_dll)
